import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/login',
        component: () => import('../pages/login.vue')
    },
    {
        path: '/index',
        component: () => import('../pages/index.vue'),
        children: [{ //首页
                path: '/home',
                component: () => import('../views/home.vue')
            },
            { //菜单管理
                path: '/menu',
                component: () => import('../views/menu.vue')
            },
            {
                path: '/role',
                component: () => import('../views/role.vue')
            },
            {
                path: '/user',
                component: () => import('../views/user.vue')
            },
            { //商品分类
                path: '/category',
                component: () => import('../views/category.vue')
            },
            { //商品规格
                path: '/specs',
                component: () => import('../views/specs.vue')
            },
            { //商品管理
                path: '/goods',
                component: () => import('../views/goods.vue')
            },
            { //会员管理
                path: '/member',
                component: () => import('../views/member.vue')
            },
            { //轮播图管理
                path: '/banner',
                component: () => import('../views/banner.vue')
            },
            { //秒杀管理
                path: '/seckill',
                component: () => import('../views/seckill.vue')
            },
            {
                path: '',
                redirect: '/home',
            }
        ]
    },
    {
        path: '*',
        redirect: '/index'
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    if (localStorage.getItem('user')) {
        next()
        return
    }
    if (to.path == '/login' || to.path == '/home') {
        next()
        return;
    }
    next('/login')
})

export default router